package com.example.express.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class HandleMapping {
    private static Map<String,MVCMapping> data = new HashMap<>();

    public static MVCMapping get(String uri){
        return data.get(uri);
    }

    public static void load(InputStream is){
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collection<Object> collections = properties.values();
        for (Object collection:collections){
            String className = (String) collection;
            try {
                Class c = Class.forName(className);
                Object obj = c.getConstructor().newInstance();
                Method[] methods = c.getMethods();
                for (Method method:methods) {
                    Annotation[] annotations = method.getAnnotations();
                    if (annotations!=null){
                        for (Annotation annotation:annotations) {
                            if (annotation instanceof ResponseBody){
                                MVCMapping mvcMapping = new MVCMapping(obj, method, ResponseType.TEXT);
                                Object m = data.put(((ResponseBody) annotation).value(), mvcMapping);
                                if (m!=null){
                                    throw new RuntimeException("请求地址重复："+((ResponseBody) annotation).value());
                                }
                            }
                            if (annotation instanceof  ResponseView) {
                                MVCMapping mvcMapping = new MVCMapping(obj,method,ResponseType.VIEW);
                                Object m = data.put(((ResponseView) annotation).value(),mvcMapping);
                                if (m!=null){
                                    throw new RuntimeException("请求地址重复："+((ResponseBody) annotation).value());
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static class MVCMapping{
        private Object object;
        private Method method;
        private ResponseType type;

        public MVCMapping() {
        }

        public MVCMapping(Object object, Method method, ResponseType type) {
            this.object = object;
            this.method = method;
            this.type = type;
        }

        public Object getObject() {
            return object;
        }

        public void setObject(Object object) {
            this.object = object;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public ResponseType getType() {
            return type;
        }

        public void setType(ResponseType type) {
            this.type = type;
        }
    }
}
